const express = require('express');
const Router = express.Router();//精简、分离路由
const model = require('./model');

const User = model.getModel('user');

Router.get('/list', function(req, res) {
    User.find({}, function(err, doc) {
        return res.json(doc);
    })
})

Router.post('/register', function(req, res) {
    console.log(req.body);
    const {user, pwd, type} = req.body;
    User.findOne({user}, function(err, doc) {
        if (doc) {
            return  res.send({
                code: 1,
                msg: '用户名重复'
            })
        }
        User.create({user, pwd, type}, function(err, doc) {
            if (err) {
                return res.send({
                    code: 1,
                    msg: '后端出错'+err
                })
            }
            return res.send({
                code: 0
            })
        })
    })
})

Router.get('/info', function(req, res) {
    return res.json({
        code: 1
    })
});

module.exports = Router;